package com.hwadee.teachingprocessmanagementsystem.controller.base;

import com.hwadee.teachingprocessmanagementsystem.dto.TableDetailDto;
import com.hwadee.teachingprocessmanagementsystem.dto.TableDto;
import com.hwadee.teachingprocessmanagementsystem.service.base.TableService;
import com.hwadee.teachingprocessmanagementsystem.util.result.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
 * @Description: 数据库表控制器
 * @Author: Jukomu
 * @Package: com.hwadee.teachingprocessmanagementsystem.controller.base
 * @Project: TeachingProcessManagementSystem
 * @name: TableController
 * @Date: 2024/7/17-23:25
 * @Filename: TableController
 */
@RestController
public class TableController {
    @Autowired
    private TableService tableService;

    /**
     * 获取所有表
     *
     * @return
     */
    @RequestMapping("/table")
    public DataResult getTables() {
        TableDto tableDto = new TableDto();
        List<String> allTables = this.tableService.getAllTables();
        tableDto.setTableList(allTables);
        System.out.println(tableDto.toString());
        return DataResult.successByData(tableDto);
    }

    @RequestMapping("/table/select")
    public DataResult getTableData(@RequestParam("tableName") String tableName, @RequestParam("entityClass") String entityClass) throws Exception {
        Class<?> type = Class.forName("com.hwadee.teachingprocessmanagementsystem.entity." + entityClass);
        List<?> tableDetail = this.tableService.getTableDetail(tableName, type);
        TableDetailDto tableDetailDto = new TableDetailDto();
        tableDetailDto.setTable(tableDetail);
        return DataResult.successByData(tableDetailDto);
    }

    @RequestMapping("/table/update")
    public DataResult updateTableData(@RequestParam("tableName") String tableName,
                                      @RequestParam("entityClass") String entityClass,
                                      @RequestBody Map<String, Object> form) throws Exception {
        Class<?> clazz = Class.forName("com.hwadee.teachingprocessmanagementsystem.entity." + entityClass); // 获取实体类类型
        System.out.println(form);

        Object entity = clazz.getDeclaredConstructor().newInstance(); // 创建实体对象
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            Object value = getValueFromFormData(field.getName(), form); // 从 formData 中获取值

            if (value != null) {
                // 类型转换
                if (field.getType() == Integer.class || field.getType() == int.class) {
                    value = Integer.parseInt(value.toString());
                } else if (field.getType() == Long.class || field.getType() == long.class) {
                    value = Long.parseLong(value.toString());
                } else if (field.getType() == Double.class || field.getType() == double.class) {
                    value = Double.parseDouble(value.toString());
                } else if (field.getType() == Float.class || field.getType() == float.class) {
                    value = Float.parseFloat(value.toString());
                } else if (field.getType() == Boolean.class || field.getType() == boolean.class) {
                    value = Boolean.parseBoolean(value.toString());
                } else if (field.getType() == String.class) {
                    value = value.toString();
                }
                // 添加更多的类型转换
            }
            field.set(entity, value); // 设置实体对象的属性值
        }

        int i = this.tableService.updateTable(tableName, entity);
        if (i > 0) {
            return DataResult.succ();
        }
        return DataResult.err();
    }

    // 从 formData 中根据字段名获取值，具体实现根据你的前端数据结构来
    private Object getValueFromFormData(String fieldName, Object formData) {
        if (formData instanceof Map) {
            Map<String, Object> formDataMap = (Map<String, Object>) formData;
            return formDataMap.get(fieldName); // 获取字段名对应的值
        }
        return null;
    }

    @RequestMapping("/table/delete")
    public DataResult deleteTableData(@RequestParam("tableName") String tableName,
                                      @RequestParam("entityClass") String entityClass,
                                      @RequestBody Map<String, Object> form) throws Exception {
        Class<?> clazz = Class.forName("com.hwadee.teachingprocessmanagementsystem.entity." + entityClass); // 获取实体类类型
        System.out.println(form);

        Object entity = clazz.getDeclaredConstructor().newInstance(); // 创建实体对象
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            Object value = getValueFromFormData(field.getName(), form); // 从 formData 中获取值

            if (value != null) {
                // 类型转换
                if (field.getType() == Integer.class || field.getType() == int.class) {
                    value = Integer.parseInt(value.toString());
                } else if (field.getType() == Long.class || field.getType() == long.class) {
                    value = Long.parseLong(value.toString());
                } else if (field.getType() == Double.class || field.getType() == double.class) {
                    value = Double.parseDouble(value.toString());
                } else if (field.getType() == Float.class || field.getType() == float.class) {
                    value = Float.parseFloat(value.toString());
                } else if (field.getType() == Boolean.class || field.getType() == boolean.class) {
                    value = Boolean.parseBoolean(value.toString());
                } else if (field.getType() == String.class) {
                    value = value.toString();
                }
                // 添加更多的类型转换
            }
            field.set(entity, value); // 设置实体对象的属性值
        }

        int i = this.tableService.deleteTable(tableName, entity);
        if (i > 0) {
            return DataResult.succ();
        }
        return DataResult.err();
    }


    @RequestMapping("/table/create")
    public DataResult createTableData(@RequestParam("tableName") String tableName,
                                      @RequestParam("entityClass") String entityClass,
                                      @RequestBody Map<String, Object> form) throws Exception {
        Class<?> clazz = Class.forName("com.hwadee.teachingprocessmanagementsystem.entity." + entityClass); // 获取实体类类型
        System.out.println(form);

        Object entity = clazz.getDeclaredConstructor().newInstance(); // 创建实体对象
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            Object value = getValueFromFormData(field.getName(), form); // 从 formData 中获取值

            if (value != null) {
                // 类型转换
                if (field.getType() == Integer.class || field.getType() == int.class) {
                    value = Integer.parseInt(value.toString());
                } else if (field.getType() == Long.class || field.getType() == long.class) {
                    value = Long.parseLong(value.toString());
                } else if (field.getType() == Double.class || field.getType() == double.class) {
                    value = Double.parseDouble(value.toString());
                } else if (field.getType() == Float.class || field.getType() == float.class) {
                    value = Float.parseFloat(value.toString());
                } else if (field.getType() == Boolean.class || field.getType() == boolean.class) {
                    value = Boolean.parseBoolean(value.toString());
                } else if (field.getType() == String.class) {
                    value = value.toString();
                }
                // 添加更多的类型转换
            }
            field.set(entity, value); // 设置实体对象的属性值
        }

        int i = this.tableService.createTable(tableName, entity);
        if (i > 0) {
            return DataResult.succ();
        }
        return DataResult.err();
    }


}
